package com.ikangtai.shecare.common.util;

import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.text.TextUtils;

import com.ikangtai.shecare.R;

public class UpdateUtil {

    private Context context;
    public static final String DOWNLOAD_APK_NAME = "shecare.apk";

    public UpdateUtil(Context context){
        this.context = context;
    }

    public int getVerCode() {
        int verCode = -1;
        try {
            verCode = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
            AppLog.i("version context.getPackageName() = " + context.getPackageName());
        } catch (PackageManager.NameNotFoundException e) {
            AppLog.e(e.getMessage());
        }
        return verCode;
    }

    public String getVerName() {
        String verName = "";
        try {
            verName = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            AppLog.e(e.getMessage());
        }
        return verName;
    }

    public int getDevType(){
        String buildType = context.getResources().getString(R.string.buildType);
        return buildType.equals("test") ? 2 : 1;
    }

    public void startDownload(String updateURL){
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(updateURL));
        //设置在什么网络情况下进行下载
        //request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);
        //设置通知栏标题
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
        request.setTitle("孕橙下载");
        request.setDescription("孕橙正在下载");
        request.setAllowedOverRoaming(false);
        //设置文件存放目录
        request.setDestinationInExternalFilesDir(context, Environment.DIRECTORY_DOWNLOADS, DOWNLOAD_APK_NAME);

        AppLog.i("下载到 Environment.DIRECTORY_DOWNLOADS = " + Environment.DIRECTORY_DOWNLOADS);

        DownloadManager downManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
        long id = downManager.enqueue(request);
    }

    public static String getChannelAPKName(Activity ctx){
        String moduleName = "shecare";
        String channelName = getChannelName(ctx);
        String buildName = ctx.getResources().getString(R.string.buildName);

        String apkName = moduleName + "-" + channelName + "-" + buildName + ".apk";

        AppLog.i("the apkName is " + apkName);
        return apkName;
    }

    /**
     * 获取渠道名
     * @param ctx 此处习惯性的设置为activity，实际上context就可以
     * @return 如果没有获取成功，那么返回值为空
     */
    public static String getChannelName(Activity ctx) {
        if (ctx == null) {
            return null;
        }
        String channelName = null;
        try {
            PackageManager packageManager = ctx.getPackageManager();
            if (packageManager != null) {
                //注意此处为ApplicationInfo 而不是 ActivityInfo,因为友盟设置的meta-data是在application标签中，而不是某activity标签中，所以用ApplicationInfo
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        channelName = applicationInfo.metaData.getString("UMENG_CHANNEL");
                    }
                }

            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        AppLog.i("the channelName is " + channelName);

        return channelName;
    }

    /**
     * 获取application中指定的meta-data
     * @return 如果没有获取成功(没有对应值,或者异常)，则返回值为空
     */
    public static String getAppMetaData(Context ctx, String key) {
        if (ctx == null || TextUtils.isEmpty(key)) {
            return null;
        }
        String resultData = null;
        try {
            PackageManager packageManager = ctx.getPackageManager();
            if (packageManager != null) {
                ApplicationInfo applicationInfo = packageManager.getApplicationInfo(ctx.getPackageName(), PackageManager.GET_META_DATA);
                if (applicationInfo != null) {
                    if (applicationInfo.metaData != null) {
                        resultData = applicationInfo.metaData.getString(key);
                    }
                }

            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        return resultData;
    }
}
